Ext.define('MyDesktop.AdminInfo', {
    extend: 'Ext.ux.desktop.Module',

    requires: [
        'Ext.data.ArrayStore',
        'Ext.util.Format',
        'Ext.grid.Panel',
        'Ext.grid.RowNumberer'
    ],

    id: 'grid-winAdminInfo',

    init: function () {
        this.launcher = {
            text: 'Admin info',
            iconCls: 'icon-grid'
        };
    },

    createWindow: function () {
        var desktop = this.app.getDesktop();
        var me = this;
        var win = desktop.getWindow('grid-winAdminInfo');
        if (!win) {

            var store = new Ext.data.JsonStore({
                storeId: 'adminInfoStore',
                proxy: {
                    type: 'ajax',
                    url: '../HotelWebService.asmx/ListAdminInfos',
                    reader: {
                        type: 'json',
                        root: 'adminInfos',
                        idProperty: 'UserCode'
                    }
                },
                fields: ['UserCode', 'UserName', 'Password', 'Phone', 'Email', { name: 'CreateDate', type: 'date'}]
            });
            win = desktop.createWindow({
                id: 'grid-winAdminInfo',
                title: AdviceLang.AdminInfo,
                width: 740,
                height: 480,
                iconCls: 'icon-grid',
                animCollapse: false,
                constrainHeader: true,
                layout: 'fit',
                items: [
                    {
                        border: false,
                        xtype: 'grid',
                        id: 'grid-adminInfo',
                        store: store,
                        columns: [
                            new Ext.grid.RowNumberer(),
                            {
                                text: AdviceLang.UserCode,
                                flex: 1,
                                width: 100,
                                sortable: true,
                                dataIndex: 'UserCode'
                            },
                            {
                                text: AdviceLang.UserName,
                                width: 100,
                                sortable: true,
                                dataIndex: 'UserName'
                            },
                            {
                                text: AdviceLang.Password,
                                width: 100,
                                sortable: true,
                                dataIndex: 'Password'
                            },
                            {
                                text: AdviceLang.Phone,
                                width: 100,
                                sortable: true,
                                dataIndex: 'Phone'
                            }, {
                                text: AdviceLang.Email,
                                width: 100,
                                sortable: true,
                                dataIndex: 'Email'
                            }, {
                                text: AdviceLang.CreateDate,
                                width: 100,
                                sortable: true,
                                dataIndex: 'CreateDate',
                                xtype: 'datecolumn',
                                format: 'Y-m-d'
                            }
                        ]
                    }
                ],
                tbar: [{
                    text: AdviceLang.Create,
                    iconCls: 'add',
                    handler: function () { me.createAdminInfo(win.items.get('grid-adminInfo'), 'Add').show(); }
                }, '-', {
                    text: AdviceLang.Edit,
                    iconCls: 'option',
                    handler: function () {
                        var gridAdminInfo = win.items.get('grid-adminInfo');
                        var recordAdminInfo = gridAdminInfo.getSelectionModel().getLastSelected();
                        if (recordAdminInfo == null || recordAdminInfo == undefined) {
                            Ext.Msg.alert(AdviceLang.AlertInfo, AdviceLang.PleaseSelectRecord);
                            return;
                        }
                        me.createAdminInfo(gridAdminInfo, 'Edit').show();
                    }
                }, '-', {
                    text: AdviceLang.Delete,
                    iconCls: 'remove',
                    handler: function () {
                        var gridAdminInfo = win.items.get('grid-adminInfo');
                        var recordAdminInfo = gridAdminInfo.getSelectionModel().getLastSelected();
                        if (recordAdminInfo == null || recordAdminInfo == undefined) {
                            Ext.Msg.alert(AdviceLang.AlertInfo, AdviceLang.PleaseSelectRecord);
                            return;
                        }
                        Ext.Ajax.request({
                            url: '../HotelWebService.asmx/DeleteAdminInfo',
                            success: function (response, opts) {
                                Ext.Msg.alert(AdviceLang.AlertInfo, AdviceLang.Success);
                                gridAdminInfo.getStore().reload();
                            },
                            failure: function (response, opts) {
                                Ext.Msg.alert(AdviceLang.AlertInfo, AdviceLang.Failed);
                            },
                            //headers: { 'my-header': 'startRehear' },
                            params: { userCode: recordAdminInfo.get('UserCode') }
                        });
                    }
                }]
            });

            store.load({
                callback: function (records, operation, success) {
                    if (success) {

                    }
                    else {

                    }
                },
                scope: this
            });
        }
        return win;
    },

    createAdminInfo: function (gridAdminInfo, type) {
        var desktop = this.app.getDesktop();
        var me = this;
        var winAdd = desktop.getWindow('win-addAdminInfo');
        var recordAdminInfo;
        if (type == 'Edit') {
            var recordAdminInfo = gridAdminInfo.getSelectionModel().getSelection();
            if (recordAdminInfo == null || recordAdminInfo == undefined || recordAdminInfo.length == 0) {
                Ext.Msg.alert(AdviceLang.AlertInfo, AdviceLang.PleaseSelectRecord);
                return;
            }
        }
        if (!winAdd) {
            var required = '<span style="color:red;font-weight:bold" data-qtip="Required">*</span>';
            winAdd = Ext.create('Ext.window.Window', {
                id: 'win-addAdminInfo',
                title: recordAdminInfo == null ? AdviceLang.AddAdminInfo : AdviceLang.EditAdminInfo,
                width: 350,
                height: 200,
                closable: false,
                iconCls: 'icon-grid',
                animCollapse: false,
                modal: true,
                constrainHeader: true,
                layout: 'fit',
                items: [
                    {
                        xtype: 'form',
                        layout: 'form',
                        id: 'addAdminInfoForm',
                        url: recordAdminInfo == null ? '../HotelWebService.asmx/CreateAdminInfo' : '../HotelWebService.asmx/UpdateAdminInfo',
                        bodyPadding: '5 5 0',
                        width: 350,
                        border: false,
                        fieldDefaults: {
                            msgTarget: 'side',
                            labelWidth: 75
                        },
                        defaultType: 'textfield',
                        items: [{
                            hidden: true,
                            name: 'UserCode',
                            submitValue: recordAdminInfo == null ? false : true,
                            value: recordAdminInfo == null ? '' : recordAdminInfo[0].get("UserCode")
                        }, {
                            fieldLabel: AdviceLang.UserName,
                            afterLabelTextTpl: required,
                            readOnly: recordAdminInfo == null ? false : true,
                            name: 'UserName',
                            value: recordAdminInfo == null ? '' : recordAdminInfo[0].get("UserName"),
                            allowBlank: false,
                            tooltip: AdviceLang.UserNameToolTip
                        }, {
                            fieldLabel: AdviceLang.Password,
                            afterLabelTextTpl: required,
                            name: 'Password',
                            value: recordAdminInfo == null ? '' : recordAdminInfo[0].get("Password"),
                            allowBlank: false,
                            tooltip: AdviceLang.PasswordToolTip
                        },
                        //                        {
                        //                            fieldLabel: AdviceLang.ConfirmPassword,
                        //                            allowBlank: false,
                        //                            submitValue: false,
                        //                            inputType: 'password',
                        //                            value: recordAdminInfo == null ? '' : recordAdminInfo.get("Password"),
                        //                            tooltip: AdviceLang.ConfirmPasswordToolTip
                        //                        }, 
                        {
                        fieldLabel: AdviceLang.Phone,
                        name: 'Phone',
                        value: recordAdminInfo == null ? '' : recordAdminInfo[0].get("Phone"),
                        tooltip: AdviceLang.Phone
                    }, {
                        fieldLabel: AdviceLang.Email,
                        name: 'Email',
                        value: recordAdminInfo == null ? '' : recordAdminInfo[0].get("Email"),
                        vtype: 'email',
                        tooltip: AdviceLang.EmailToolTip
                    }, {
                        hidden: true,
                        name: 'CreateDate',
                        submitValue: recordAdminInfo == null ? false : true,
                        value: recordAdminInfo == null ? '' : recordAdminInfo[0].get("CreateDate")
                    }],

                    buttons: [{
                        text: AdviceLang.Save,
                        handler: function () {
                            var form = this.up('form').getForm();
                            var vauleObj = form.getValues();
                            if (form.isValid()) {
                                form.submit({
                                    success: function (form, action) {
                                        //Ext.Msg.alert(AdviceLang.AlertInfo, AdviceLang.Success);
                                        winAdd.close();
                                        //gridAdminInfo.getStore().removeAll();
                                        gridAdminInfo.getStore().load();
                                        //var index = gridAdminInfo.getStore().indexOf(recordAdminInfo[0]);
                                        //gridAdminInfo.getSelectionModel().select(index - 1);
                                        gridAdminInfo.getSelectionModel().deselectAll();
                                        //gridAdminInfo.getSelectionModel().select(index);
                                    },
                                    failure: function (form, action) {
                                        Ext.Msg.alert(AdviceLang.AlertInfo, AdviceLang.Failed);
                                    }
                                });
                            }
                        }
                    }, {
                        text: AdviceLang.Cancel,
                        handler: function () {
                            winAdd.close();
                        }
                    }]
                }
                ]
            })

            return winAdd;
        }
    }
});





